package com.leps.nq.sys.controller;

import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import com.leps.nq.sys.entity.SysMenu;
import com.leps.nq.sys.service.ISysMenuService;
import com.leps.nq.sys.vo.CreateMenuVo;
import com.leps.nq.sys.vo.MenuInfoVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 菜单权限表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-05-20
 */
@RestController
@RequestMapping("/sys/sys-menu")
@Api(tags = "菜单管理")
public class SysMenuController {
    @Autowired
    private ISysMenuService menuService;

    @ApiOperation("获取系统所有菜单(树形结构)")
    @GetMapping("/tree")
    public R<List<MenuInfoVo>> menuTree() {
        List<MenuInfoVo> list = this.menuService.selectMenusAsTree();
        return RBuilder.ok(list);
    }

    @ApiOperation("获取系统所有菜单(线性结构)")
    @GetMapping("/list")
    public R<List<SysMenu>> menuList() {
        List<SysMenu> list = this.menuService.selectMenusAsList();
        return RBuilder.ok(list);
    }

    @ApiOperation("根据父级ID查找子级菜单")
    @GetMapping("/children")
    public R<List<SysMenu>> childrenOf(Long parentId) {
        List<SysMenu> children = this.menuService.selectChildrenByParentId(parentId);
        return RBuilder.ok(children);
    }

    @ApiOperation("根据父级ID深度查找后代菜单ID")
    @GetMapping("/deepChildren")
    public R<List<Long>> deepChildrenOf(Long parentId) {
        List<Long> list = this.menuService.selectDeepChidrenByParentId(parentId);
        return RBuilder.ok(list);
    }

    @ApiOperation("根据ID查找菜单信息")
    @GetMapping("/{menuId}")
    public R<SysMenu> getById(@PathVariable Long menuId) {
        SysMenu sysMenu = this.menuService.getById(menuId);
        return RBuilder.ok(sysMenu);
    }

    @ApiOperation("创建菜单")
    @PostMapping
    @Log(title = "系统菜单", businessType = BusinessType.INSERT)
    public R save(@RequestBody CreateMenuVo createMenuVo) {
        boolean b = this.menuService.createMenu(createMenuVo);
        return RBuilder.ofBoolean(b);
    }

    @ApiOperation("更新菜单信息")
    @PutMapping()
    @Log(title = "系统菜单", businessType = BusinessType.UPDATE)
    public R update(@RequestBody SysMenu sysMenu) {
        boolean b = this.menuService.updateMenu(sysMenu);
        return RBuilder.ofBoolean(b);
    }

    @ApiOperation("显示/隐藏菜单(0显示 1隐藏)")
    @PutMapping("/changeVisible")
    @Log(title = "系统菜单", businessType = BusinessType.UPDATE)
    public R changeVisible(Long menuId, String visible) {
        boolean b = this.menuService.changeVisible(menuId, visible);
        return RBuilder.ofBoolean(b);
    }

    @ApiOperation("启用/停用菜单(0正常 1停用)")
    @PutMapping("/changeStatus")
    @Log(title = "系统菜单", businessType = BusinessType.UPDATE)
    public R changeStatus(Long menuId, String status) {
        boolean b = this.menuService.changeStatus(menuId, status);
        return RBuilder.ofBoolean(b);
    }

    @ApiOperation("删除菜单")
    @DeleteMapping
    @Log(title = "系统菜单", businessType = BusinessType.DELETE)
    public R remove(@RequestBody Long[] menuIds) {
        boolean b = this.menuService.removeMenus(menuIds);
        return RBuilder.ofBoolean(b);
    }
}
